<?php

define(C("ACCESSACTION"), true);
import('jinghuan.AdminAction');
import('jinghuan.Func');

/**
 * Class ApiAction
 */
class UploadAction extends AdminAction
{
//    var $upload_filter = array('gif', 'jpg', 'jpeg', 'png', 'bmp', 'avi', 'dat', 'flv', 'mkv', 'wmv', 'mov', 'rmvb', 'rm', 'mp3', 'mp4', 'txt', 'docx');

    var $upload_filter = array('gif', 'jpg', 'jpeg', 'png', 'apk');

    var $upload_size = 1024 * 100;

    /**
     * 检查用户是否登录
     */
    protected function _check_login()
    {
        //TODO 接口开发阶段
        return true;
    }

    /**
     * banner 上传图片
     */
    function get_banner()
    {
//        ob_clean();
        $post = I("request.");

        $filePath = $post['id'];

        // 创建目录
        createdir(APP_PATH . '/Source/scdc/banner/' . $filePath);

        // 保存图片
        $SavePath = clean_path(APP_PATH . '/Source/scdc/banner/');

        $error = $this->SaveUpload($SavePath, $filePath . '/' . date('YmdHis'));
        if (is_string($error)) {
            $this->callback(false, array(errmsg => $error));
            return;
        }

        D(JH_BANNER)->save(
            array(
                id => $post['id'],
                u_time => time(),
                img => $error['file']
            )
        );
        $data = D(JH_BANNER)->find($post['id']);
        $this->callback($data);
    }

    /**
     * 头像
     */
    function get_header()
    {
//        ob_clean();
        $post = I("request.");

        // 文件相对路径
        $filePath = $post['id'];

        // 创建目录
        createdir(APP_PATH . '/Source/scdc/header/' . $filePath);

        // 文件根路径
        $SavePath = clean_path(APP_PATH . '/Source/scdc/header/');

        // 保存图片 文件以 年月日时分秒命名 例如 20160106161030
        $error = $this->SaveUpload($SavePath, $filePath . '/' . date('YmdHis'));
        if (is_string($error)) {
            $this->callback(false, array(errmsg => $error));
            return;
        }

        D(JH_SOURCE_CLIENT)->save(
            array(
                id => $post['id'],
                u_time => time(),
                img => $error['file']
            )
        );

        $data = D(JH_SOURCE_CLIENT)->find($post['id']);
        $this->callback($data);
    }

    /**
     * 文章展示图
     */
    function get_article()
    {
//        ob_clean();
        $post = I("request.");

        // 文件相对路径
        $filePath = $post['id'];

        // 创建目录
        createdir(APP_PATH . '/Source/scdc/article/' . $filePath);

        // 文件根路径
        $SavePath = clean_path(APP_PATH . '/Source/scdc/article/');

        // 保存图片 文件以 年月日时分秒命名 例如 20160106161030
        $error = $this->SaveUpload($SavePath, $filePath . '/' . date('YmdHis'));

        if (is_string($error)) {
            $this->callback(false, array(errmsg => $error));
            return;
        }

        D(JH_ARTICLE)->save(
            array(
                id => $post['id'],
                u_time => time(),
                img => $error['file']
            )
        );

        $data = D(JH_ARTICLE)->find($post['id']);
        $this->callback($data);
    }


    /**
     * banner 上传apk
     */
    function get_apk()
    {

//        ob_clean();
        $post = I("request.");

        // 创建目录
        createdir(APP_PATH . '/Source/scdc/apk/');

        // 保存apk
        $SavePath = clean_path(APP_PATH . '/Source/scdc/apk/');

        $error = $this->SaveUpload($SavePath);

        if (is_string($error)) {
            $this->callback(false, array(errmsg => $error));
            return;
        }

        D(JH_UPDATE)->save(
            array(
                id => $post['id'],
                u_time => time(),
                img => $error['file']
            )
        );
        $data = D(JH_UPDATE)->find($post['id']);
        $this->callback($data);
    }

}